//
//  main.m
//  06_Homework
//
//  Created by 徐昊 on 16/7/8.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "City.h"
#import "Province.h"
#import "Town.h"
#import "Zone.h"
#import "TempCity.h"
void homeWork1 () {
//    一、
//    1.创建1个包含5个字符串元素的数组；
//    2.用for循环倒序输出所有元素；
//    3.用for...in完成所有字符串元素的拼接(生成一个
//    新的字符串)，并输出拼接好的字符串。
    NSArray *arr = @[@"1", @"2", @"3", @"4", @"5"];
    for (NSInteger i = arr.count - 1; i >= 0; i--) {
        NSLog(@"%@", arr[i]);
    }
    NSMutableString * appendStr = [[NSMutableString alloc] init];
    for (NSString *str in arr) {
        [appendStr appendString:str];
    }
    NSLog(@"%@", appendStr);
}
void homeWork2 () {
//    二、
//    1.创建1个包含5个person对象的数组(姓名用英文)；
//    2.用for循环找到年龄最大的Person对象；
//    3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
    Person *per1 = [Person personWithName:@"Tom" age:29 gender:@"male"];
    Person *per2 = [Person personWithName:@"Amy" age:30 gender:@"female"];
    Person *per3 = [Person personWithName:@"LiLei" age:15 gender:@"male"];
    Person *per4 = [Person personWithName:@"HanMeimei" age:16 gender:@"female"];
    Person *per5 = [Person personWithName:@"Yamato" age:19 gender:@"female"];
    NSArray *personArray = @[per1, per2, per3, per4, per5];
    NSInteger maxAge = 0;
    int site = 0;
    for (int i = 0; i < personArray.count; i++) {
        if ([personArray[i] age] >= maxAge) {
            maxAge = [personArray[i] age];
            site = i;
        }
    }
    NSLog(@"%@是年龄最大的人", personArray[site]);
    for (Person *per in personArray) {
        if (per.age > 20) {
            per.name = [per.name stringByAppendingString:@"-brother"];
        }
    }
    NSLog(@"%@", personArray);
    //    三、
    //    1.使用第二题目中生成的数组；
    //    2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
    //    3.使用NSSortDescriptor，按姓名进行排序。
    personArray = [personArray sortedArrayUsingSelector:@selector(compareWithName:)];
    NSLog(@"%@", personArray);
    personArray = [personArray sortedArrayUsingSelector:@selector(compareWithAge:)];
    NSLog(@"%@", personArray);
    personArray = [personArray sortedArrayUsingSelector:@selector(compareWithSex:)];
    NSLog(@"%@", personArray);
    NSSortDescriptor *perSort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSSortDescriptor *perSort2 = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
    NSSortDescriptor *perSort3 = [[NSSortDescriptor alloc] initWithKey:@"gender" ascending:YES];
    personArray = [personArray sortedArrayUsingDescriptors:@[perSort]];
    NSLog(@"%@", personArray);
    personArray = [personArray sortedArrayUsingDescriptors:@[perSort2]];
    NSLog(@"%@", personArray);
    personArray = [personArray sortedArrayUsingDescriptors:@[perSort3]];
    NSLog(@"%@", personArray);
}
void homeWork3 () {
    //    四、
    //    处理省市区文件中的area.txt将省市区进行分割处理, 省里包含市, 市里包含区
    //    1. 先用数组字典表示
    //    2. 将数组字典的处理方式转换成数组模型(Class)
    // {北京:{北京市:[西城区, 东城区], 市:[区, 区]}]
    //  上海:[{上海市:[东门, 西门], ..市:[]}
    NSMutableArray *cityArr = [NSMutableArray array];
    NSString * dataString = [NSString stringWithContentsOfFile:@"/Users/xuhao/DLS160710/Objective-C/HomeWork/06_Homework/06_Homework/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *chinaArr = [dataString componentsSeparatedByString:@"\n"];
    int temp = 0;
    int temp1 = 0;
    NSMutableDictionary *province = [NSMutableDictionary dictionary];
    NSMutableDictionary *city = [NSMutableDictionary dictionary];
    NSMutableArray *town = [NSMutableArray array];
    for (int i = 0; i < chinaArr.count; i++) {
        if (![chinaArr[i] hasPrefix:@"  "]) {
            temp = i;
        } else if ([chinaArr[i] hasPrefix:@"  "] && ![chinaArr[i] hasPrefix:@"    "])
        {
            temp1 = i;
        } else {
            if ([province objectForKey:chinaArr[temp]] == nil) {
                city = [NSMutableDictionary dictionary];
                town = [NSMutableArray arrayWithObject:chinaArr[i]];
                [city setObject:town forKey:chinaArr[temp1]];
                [province setObject:city forKey:chinaArr[temp]];
                [cityArr addObject:[City cityWtihProvince:chinaArr[temp] city:chinaArr[temp1] town:chinaArr[i]]];
            } else {
                if ([city objectForKey:chinaArr[temp1]] == nil) {
                    town = [NSMutableArray arrayWithObject:chinaArr[i]];
                    [city setObject:town forKey:chinaArr[temp1]];
//                    [province setObject:city forKey:chinaArr[temp]];
                    [cityArr addObject:[City cityWtihProvince:chinaArr[temp] city:chinaArr[temp1] town:chinaArr[i]]];
                } else {
                    [town addObject:chinaArr[i]];
//                    [city setObject:town forKey:chinaArr[temp1]];
//                    [province setObject:city forKey:chinaArr[temp]];
                    [cityArr addObject:[City cityWtihProvince:chinaArr[temp] city:chinaArr[temp1] town:chinaArr[i]]];
                }
            }
        }
    }
    NSLog(@"%@", province);
//    NSLog(@"%@", cityArr);
}
void homeWork4 () {
//    四、
//    处理省市区文件中的area.txt将省市区进行分割处理, 省里包含市, 市里包含区
//    1. 先用数组字典表示
//    2. 将数组字典的处理方式转换成数组模型(Class)
    // {北京:[{北京市:[西城区, 东城区]}]}
    NSString * dataString = [NSString stringWithContentsOfFile:@"/Users/xuhao/DLS160710/Objective-C/HomeWork/06_Homework/06_Homework/area.txt" encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"%@", dataString);
    NSArray *chinaArr = [dataString componentsSeparatedByString:@"\n"];
//    NSLog(@"%@", chinaArr);
    int temp = 0;
    int temp1 = 0;
    NSMutableDictionary *province = [NSMutableDictionary dictionary];
    for (int i = 0; i < chinaArr.count; i++) {
        if (![chinaArr[i] hasPrefix:@"  "]) {
//            NSMutableArray *city = [NSMutableArray array];
//            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:city forKey:chinaArr[i]];
//            NSLog(@"%@", chinaArr[i]);
//            NSLog(@"%@", dic);
            temp = i;
        } else if ([chinaArr[i] hasPrefix:@"  "] && ![chinaArr[i] hasPrefix:@"    "])
        {
//            NSMutableArray *town = [NSMutableArray array];
//            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:town forKey:chinaArr[i]];
//            [[province objectForKey:chinaArr[temp]] addObject:dic];
//            NSLog(@"%@", chinaArr[temp]);
//            NSLog(@"%@", [province objectForKey:chinaArr[temp]]);
//            NSLog(@"%@", chinaArr[i]);
            temp1 = i;
        } else {
//            [[[province objectForKey:chinaArr[temp]] objectForKey:chinaArr[temp1]] addObject:chinaArr[i]];
//            NSLog(@"%@", chinaArr[i]);
        }
    }
    NSLog(@"%@", province);
}
void homework5 () {
//    五、拓展
//    上述作业做完, 处理Location1.txt要求同第四题。自己寻找规律
    NSString *source = [NSString stringWithContentsOfFile:@"/Users/xuhao/DLS160710/Objective-C/HomeWork/06_Homework/06_Homework/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
    // 根据, 分割字符串, 得到所有城市的数组
    NSArray *allCityArray = [source componentsSeparatedByString:@","];
    NSMutableArray *allCityDicArray = [NSMutableArray array];
    // 根据每个城市中的|处理字符, 重新生成字典的数组
    for (NSString *city in allCityArray) {
        // 根据|切割字符串
        NSArray *arrayCity = [city componentsSeparatedByString:@"|"];
        // 根据切割生成的数组, 生成新的城市字典
        NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[arrayCity firstObject], @"cityName", [arrayCity lastObject], @"cityNumber", nil];
        // 添加到新数组中
        [allCityDicArray addObject:cityDic];
    }
    
    // 排序(重要)
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allCityDicArray sortUsingDescriptors:@[sort]];
//    NSLog(@"%@", allCityDicArray);
    
    NSMutableArray *finalArray = [NSMutableArray array];
    for (NSMutableDictionary *cityDic in allCityDicArray) {
        NSString *cityName = cityDic[@"cityName"];
        NSString *cityNumber = cityDic[@"cityName"];
        if ([cityNumber hasSuffix:@"0000"]) {
            NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [proDic setObject:[NSMutableArray array] forKey:@"cityArray"];
            [finalArray addObject:proDic];
        } else if ([cityNumber hasSuffix:@"00"]) {
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [dicCity setObject:[NSMutableArray array] forKey:@"zoneArray"];
            [cityArray addObject:dicCity];
        } else {
//            NSMutableArray *zoneArray = [[[[finalArray lastObject] objectForKey:@"cityArray"] lastObject] objectForKey:@"zoneArray"];
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [cityArray lastObject];
            NSMutableArray *zoneArray = [dicCity objectForKey:@"zoneArray"];
            NSMutableDictionary *zoneDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [zoneArray addObject:zoneDic];
        }
    }
    
    /** tempCity */
    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in allCityArray) {
        // 根据|切割字符串
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        TempCity *tempCity = [[TempCity alloc] initWithCityName:[city firstObject] cityNumber:[city lastObject]];
        [allTempCityArray addObject:tempCity];
    }
    // 对 类数组 排序
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sortTempCity]];
    NSLog(@"%@", allTempCityArray);
    
    NSMutableArray *finalClassArray = [NSMutableArray array];
    for (TempCity *tempCity in allTempCityArray) {
        if ([tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [Province zoneWithTempCity:tempCity];
            [finalClassArray addObject:pro];
        } else if ([tempCity.cityNumber hasPrefix:@"00"] && ![tempCity.cityNumber hasPrefix:@"0000"]) {
            Province *pro = [finalClassArray lastObject];
            Town *city = [Town zoneWithTempCity:tempCity];
            [pro.zoneArray addObject:city];
        } else {
            Province *pro = [finalClassArray lastObject];
            Town *city = [pro.zoneArray lastObject];
            Zone *zone = [Zone zoneWithTempCity:tempCity];
            [city.zoneArray addObject:zone];
        }
    }
}
//    六、如果都做完额外找我要作业, 练习有的是, 就怕你不练
    
void homework6 () {
    NSString * dataString = [NSString stringWithContentsOfFile:@"/Users/xuhao/DLS160710/Objective-C/HomeWork/06_Homework/06_Homework/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *allArray = [dataString componentsSeparatedByString:@"\n"];
    NSMutableArray *allProvinceArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        // 取到所有的省(没有空格开头)
        if (![area hasPrefix:@"  "]) {
            // 创建了省字典(字典中包含两个字段 name array)
            NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
            // 添加proName字段
            [proDic setObject:area forKey:@"proName"];
            // 创建了省中所有事的数组
            NSMutableArray *cityArray = [NSMutableArray array];
            // 添加数组字段
            [proDic setObject:cityArray forKey:@"cityArray"];
            // 将创建好的省字典, 放到省数组中
            [allProvinceArray addObject:area];
            // 取到所有的市(两个空格开头, 但是不是四个空格开头)
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            // 难点: 通过 lastObject 获取到对应的省字典
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
            // 拿到省字典中对应的市数组
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            // 创建 市字典(name 和 区array)
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:area forKey:@"cityName"];
            NSMutableArray *zoneArray = [NSMutableArray array];
            [cityDic setObject:zoneArray forKey:@"zoneArray"];
            // 向市数组中添加字典
            [cityArray addObject:cityDic];
            // 取到所有的县(四个空格开头)
        } else {
            // 通过市数组获取到对应的市字典
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
            // 通过市数组获取到市数组
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            // 通过市数组获取到对应的市字典
            NSMutableDictionary *city = [cityArray lastObject];
            // 通过市字典对应的区/县数组
            NSMutableArray *zoneArray = [city objectForKey:@"zoneArray"];
            // 最终将区/县添加到数组中
            [zoneArray addObject:area];
        }
    }
    NSLog(@"%@", allProvinceArray);
}

void province_city_twon () {
    NSString * dataString = [NSString stringWithContentsOfFile:@"/Users/xuhao/DLS160710/Objective-C/HomeWork/06_Homework/06_Homework/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *allArray = [dataString componentsSeparatedByString:@"\n"];
    // 2.省数组 - Province类
    NSMutableArray *provinceObjectArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            Province *prov = [Province zoneWithName:area];
            [provinceObjectArray addObject:prov];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *prov = [provinceObjectArray lastObject];
            Town *city = [Town zoneWithName:area];
            [prov.zoneArray addObject:city];
        } else {
            Town *city = [[[provinceObjectArray lastObject] array] lastObject];
            Zone *town = [Zone zoneWithName:area];
            [city.zoneArray addObject:town];
        }
    }
    NSLog(@"%@", provinceObjectArray);
    
}
    
int main(int argc, const char * argv[]) {
//    homeWork1();
//    homeWork2();
//    homeWork3();
//    homeWork4();
//    homework5();
//    homework6();
//    province_city_twon();
    
    return 0;
}
